#include<iostream>
#include<ctime>
using namespace std;
#include"BlockQueue.hpp"
#include<pthread.h>
#include<unistd.h>
#include"task.hpp"

void* consumer(void* bp)
{
    BlockQueue<task>* b = static_cast<BlockQueue<task>*>(bp);
    while(1)
    {
        task t;

        b->pop(&t);
        t.run();
        
        cout<<"consumer: "<<t.ConsumerMes()<<endl;
        // int mes = 0;
        // b->pop(&mes);
        // cout<<"consumer get: "<< mes <<endl;
    }
    return nullptr;
}


void* product(void* bp)
{
    BlockQueue<task>* b = static_cast<BlockQueue<task>*>(bp);
    while(1)
    {
        int x = rand()%10;
        usleep(rand()%123);
        int y = rand()%10;
        usleep(rand()%123);
        char oper = opers[rand()% opers.size()];

        task t(x,y,oper);
        b->push(t);

        cout<<"productor: "<<t.ProductorMes()<<endl;
        
        // int mes = rand()%10+1;
        // b->push(mes);
        // cout<<"productor send: "<< mes <<endl;
        // sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((size_t)pthread_self()& 12334^3456);
    BlockQueue<task>* bp = new BlockQueue<task>;
    pthread_t c,p;
    pthread_create(&c,nullptr,consumer,bp);
    pthread_create(&p,nullptr,product,bp);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    return 0;
}
